package Sync
{
	import flash.errors.IOError;
	import flash.utils.ByteArray;
	
	public class Stream
	{
		public static var TYPE_INT:int = 2;
		public static var TYPE_FLOAT:int = 3;
		public static var TYPE_STRING:int = 4;
		
		private var byteArray:ByteArray;
		
		public function setByteArray(bytes:ByteArray):void
		{
			byteArray = bytes;
		}
		
		public function getByteArray():ByteArray
		{
			return byteArray;
		}
		
		private function checkType(type:int):void
		{
			var tmp:int = byteArray.readByte();
			if (tmp != type)
			{
				throw new IOError("net stream type error");
			}
		}
		
		private function writeType(type:int):void
		{
			byteArray.writeByte(type);
		}
		
		public function readInt():int
		{
			checkType(TYPE_INT);
			
			return byteArray.readInt();
		}
		
		public function readFloat():Number
		{
			checkType(TYPE_FLOAT);
			
			return byteArray.readFloat();
		}
		
		public function readString():String
		{
			checkType(TYPE_STRING);
			
			return byteArray.readUTF();
		}
		
		public function writeInt(value:int):void
		{
			writeType(TYPE_INT);			
			
			byteArray.writeInt(value);
		}
		
		public function writeFloat(value:Number):void
		{
			writeType(TYPE_FLOAT);			
			
			byteArray.writeFloat(value);
		}
		
		public function writeString(value:String):void
		{
			writeType(TYPE_STRING);			
			
			byteArray.writeUTF(value);
		}
		
		public function writeByteArray(value:ByteArray):void
		{
			byteArray.writeBytes(value);
		}
	}
}